package com.example.demo.filter;

import com.alibaba.fastjson.JSON;
import com.example.demo.dto.ResultBean;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestControllerAdvice;

@RestControllerAdvice
public class InterfaceExceptionHandler {

    /**
     * 接口 业务异常
     */
    //    @ResponseBody
    //    @ExceptionHandler(BusinessInterfaceException.class)
    //    public String businessInterfaceException(BusinessInterfaceException e) {
    //        log.error(e.getMessage(), e);
    //        ErrorBean error = e.getError();
    //        ResultBean resultBean = new ResultBean(error.hashCode(), error.getErrorMsg());
    //        return resultBean.toString();
    //    }

    /**
     * 拦截所有运行时的全局异常   
     */
    @ExceptionHandler(RuntimeException.class)
    @ResponseBody
    public ResultBean runtimeException(RuntimeException e) {
        // log.error(e.getMessage(), e);
        // 返回 JOSN
         return new ResultBean(false, 501);
    }

    /**
     * 系统异常捕获处理
     */
    @ExceptionHandler(Exception.class)
    @ResponseBody
    public ResultBean exception(Exception e) {
        //log.error(e.getMessage(), e);
        //ResultBean resultBean = new ResultBean(Constants.INTERFACE_MSG_301, Constants.INTERFACE_MSG_301_TEXT);
        // 返回 JOSN
        //return resultBean.toString();
        return new ResultBean(false, 500);
    }
}